package com.example.teadoctor;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Records extends AppCompatActivity {
    List<String> pageList;
    ArrayAdapter<String> adapter;
    SharedPreferences sharedPreferences;
    ListView listView;
    Button deleteButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_records);

        listView = findViewById(R.id.Pst_my_records);
        //获取浏览的页面标题
        sharedPreferences = getSharedPreferences("BrowsingHistory", MODE_PRIVATE);
        String[] pages = Objects.requireNonNull(sharedPreferences.getString("VisitedPages", "")).split(",");
        pageList = new ArrayList<>();
        for (int i = pages.length - 1; i >= 0; i--) {
            if (!pages[i].isEmpty()) {
                pageList.add(pages[i]);
            }
        }

        //ListView展示浏览记录
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, pageList);

        try {    listView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //点击记录跳转功能
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = pageList.get(position);
                String[] parts = selectedItem.split(" {14}");
                String selectedName = parts[0];

                if (selectedName.length() <= 5) {
                    // 将茶名通过intent传递给要跳转的页面
                    Intent intent = new Intent(Records.this, Information.class);
                    intent.putExtra("name", selectedName);
                    startActivity(intent);
                } else if(selectedName.startsWith("如何")){
                    // 将经营标题通过intent传递给要跳转的页面
                    Intent intent = new Intent(Records.this, BusinessInformation.class);
                    intent.putExtra("title", selectedName);
                    startActivity(intent);
                }
            }
        });

        //长按删除浏览记录功能
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Records.this);
                builder.setMessage("确定要删除该记录吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                pageList.remove(position);
                                adapter.notifyDataSetChanged();

                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("VisitedPages", TextUtils.join(",", pageList));
                                editor.apply();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
                return true;
            }
        });

        //全部删除按钮
        deleteButton = findViewById(R.id.btn_deleteRecords);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Records.this);
                builder.setMessage("确定删除所有浏览记录吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.remove("VisitedPages");
                            editor.apply();
                            try {
                            pageList.clear();

                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.show();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        /*和删除，跳转功能有所冲突
        //页面发生改变时，重新读取SharedPreferences中的数据，更新浏览记录
        SharedPreferences sharedPreferences = getSharedPreferences("BrowsingHistory", MODE_PRIVATE);
        String[] pages = sharedPreferences.getString("VisitedPages", "").split(",");
        List<String> pageList = new ArrayList<>();
        for (int i = pages.length - 1; i >= 0; i--) {
            if (!pages[i].isEmpty()) {
                pageList.add(pages[i]);
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pageList);
        ListView listView = findViewById(R.id.Pst_my_records);
        listView.setAdapter(adapter);
        */
    }
}
